import * as api from './api';
import {PAGE_CODE} from './constants';
import {PAGE} from '../constants';
import {
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi
} from 'Shared/actions/currentUserContext';
export const ERROR_CODE = 'error';
export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';
import {conventTableCondition} from '../utils';
import {isYesNo} from '../Enum';
const getInitDataBegin = () => ({
    type: GET_INIT_DATA_BEGIN
});

const getInitDataSuccess = data => ({
    type: GET_INIT_DATA_SUCCESS,
    data
});
const getInitDataFail = () => ({
    type: GET_INIT_DATA_FAIL
});
/**初始化 */
/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';
const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});

const getListDataSuccess = (condition, data) => ({
    type: GET_LIST_DATA_SUCCESS,
    condition,
    data
});

const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});

/*==============================初始化车型================================= */
export const GET_INIT_CATEGORY_DATA_BEGIN = 'GET_INIT_CATEGORY_DATA_BEGIN';
export const GET_INIT_CATEGORY_DATA_SUCCESS = 'GET_INIT_CATEGORY_DATA_SUCCESS';
export const GET_INIT_CATEGORY_DATA_FAIL = 'GET_INIT_CATEGORY_DATA_FAIL';
const getInitCategoryDataBegin = () => ({
    type: GET_INIT_CATEGORY_DATA_BEGIN
});

const getInitCategoryDataSuccess = data => ({
    type: GET_INIT_CATEGORY_DATA_SUCCESS,
    data
});
const getInitCategoryDataFail = () => ({
    type: GET_INIT_CATEGORY_DATA_FAIL
});
/*==============================初始化API================================= */
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInitData().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        else
            dispatch(getInitDataFail());
        return res.ok;
    });
    dispatch(getInitCategoryDataBegin());
    const category = api.productCategoryQuery().then(res => {
        if(res.ok)
            dispatch(getInitCategoryDataSuccess(res.data));
        else
            dispatch(getInitCategoryDataFail());
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });

    return Promise.all([init, permissions, category]).then(values => values[0] && values[1] && values[2]);
};

/*==============================查询action================================= */
const convent = (condition = {}) => {
    const {isSales, isKd, ...others} = condition;
    return {
        ...others,
        // eslint-disable-next-line no-nested-ternary
        isSales: isSales === isYesNo.是 ? true : (isSales === isYesNo.否 ? false : undefined),
        // eslint-disable-next-line no-nested-ternary
        isKd: isKd === isYesNo.是 ? true : (isKd === isYesNo.否 ? false : undefined),
    };
};
//获取查询列表
export const getList = condition => dispatch => {
    dispatch(getListDataBegin());
    const queryCondition = (conventTableCondition(convent(condition)));
    const {saleDate, rolloutDate, ...props} = queryCondition;
    if(saleDate && saleDate.length > 0) {
        props.beginSaleDate = saleDate[0];
        props.endSaleDate = saleDate[1];
    }
    if(rolloutDate && rolloutDate.length > 0) {
        props.beginRolloutDate = rolloutDate[0];
        props.endRolloutDate = rolloutDate[1];
    }
    return api.getList(props).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(condition, res.data));
        else
            dispatch(getListDataFail());
    });
};
/*=============================获取详情================================== */
export const GET_DETAIL_BEGIN = 'GET_DETAIL_BEGIN';
export const GET_DETAIL_SUCCESS = 'GET_DETAIL_SUCCESS';
export const GET_DETAIL_FAIL = 'GET_DETAIL_FAIL';
export const getDetailBegin = () => ({
    type: GET_DETAIL_BEGIN,
});

export const getDetailSuccess = data => ({
    type: GET_DETAIL_SUCCESS,
    data,
});

const getDetailFail = () => ({
    type: GET_DETAIL_FAIL,
});

//获取详情
export const getDetail = id => dispatch => {
    dispatch(getDetailBegin());
    return api.getDetail(id).then(res => {
        if(res.ok)
            dispatch(getDetailSuccess(res.data));
        else
            dispatch(getDetailFail());
    });
};
//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const pageTableCondition = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    condition.pageSize = pageTableCondition.pageSize || PAGE.size;
    condition.sortedColumnKey = pageTableCondition.sortedColumnKey;
    condition.sortedOrder = pageTableCondition.sortedOrder;
    dispatch(getList(condition));
};
//分页排序搜索
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    if(!query.pageIndex || !page)
        query.pageIndex = 0;
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

/*==============================导出================================= */
export const exportData = () => (dispatch, getState) => {
    const queryCondition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const {saleDate, rolloutDate, ...props} = queryCondition;
    if(saleDate && saleDate.length > 0) {
        props.beginSaleDate = saleDate[0];
        props.endSaleDate = saleDate[1];
    }
    if(rolloutDate && rolloutDate.length > 0) {
        props.beginRolloutDate = rolloutDate[0];
        props.endRolloutDate = rolloutDate[1];
    }
    return api.exports(convent(props));
};

//===========================编辑action==============================
export const onSubmit = data => () => {
    const updateCondition = {
        rowVersion: data.rowVersion,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        licensePlate: data.licensePlate,
        nextMaintenanceTime: data.nextMaintenanceTime,
        vehicleClaimType: data.vehicleClaimType,
        powerType: data.powerType,
        transmissionType: data.transmissionType,
        remark: data.remark
    };
    return data.id && api.modifyOrder(data.id, updateCondition)
        .then(res => ({success: res.ok}));
};
/*==============================导出================================= */
export const exportKdData = () => (dispatch, getState) => {
    const queryCondition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const {saleDate, rolloutDate, ...props} = queryCondition;
    if(saleDate && saleDate.length > 0) {
        props.beginSaleDate = saleDate[0];
        props.endSaleDate = saleDate[1];
    }
    if(rolloutDate && rolloutDate.length > 0) {
        props.beginRolloutDate = rolloutDate[0];
        props.endRolloutDate = rolloutDate[1];
    }
    return api.exportKds(convent(props));
};
